﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using umbCache = umbraco.cms.businesslogic.cache.Cache;


namespace TwentyTree4Umbraco
{
    public class Config
    {

        public static string GetSetting(string key)
        {
            return umbCache.GetCacheItem<string>("TwentyTree4Umbraco" + key, new object(), TimeSpan.FromHours(24),
                delegate
                {
                    XmlDocument config = new XmlDocument();
                    config.Load(
                        HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/plugins/TwentyTree4Umbraco/TwentyTree4Umbraco.config"));

                    XmlNode node =
                        config.SelectSingleNode(
                        string.Format("/TwentyTree4Umbraco/Settings/setting [@key = '{0}']", key));

                    if (node != null)
                        return node.Attributes["value"].Value;
                    else
                        return "";
                });
        }

        public static XmlNodeList GetPages()
        {
            return umbCache.GetCacheItem<XmlNodeList>("TwentyTree4UmbracoPage", new object(), TimeSpan.FromHours(24),
                 delegate
                 {
                     XmlDocument config = new XmlDocument();
                     config.Load(
                         HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/plugins/TwentyTree4Umbraco/TwentyTree4Umbraco.config"));

                     return config.SelectNodes("/TwentyTree4Umbraco/Section/page");
                 });
        }
    }
}
